//
//  CZJCOAuthController.swift
//  SinaWeibo
//
//  Created by mac on 15/10/7.
//  Copyright © 2015年 cz. All rights reserved.
//

import UIKit
import SVProgressHUD

class CZJCOAuthController: UIViewController,UIWebViewDelegate  {

    let WB_client_id = "2518243564"
    let WB_redirect_uri = "http://www.baidu.com"
    let pageURL = "https://api.weibo.com/oauth2/authorize"
    
    var webView: UIWebView{
        return view as! UIWebView
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        loadOAuthPage()
        
    }
    //加载网页授权
   private func loadOAuthPage(){
        let urlString = pageURL + "?" + "client_id=" + WB_client_id +  "&redirect_uri=" + WB_redirect_uri
        let url = NSURL(string: urlString)!
        
        webView.loadRequest(NSURLRequest(URL: url))
    }
    //关闭
    @IBAction func close() {
        SVProgressHUD.dismiss()
        dismissViewControllerAnimated(true, completion: nil)
    }
    //默认账号自动填充
    @IBAction func defaultAccount(sender: AnyObject) {
        let jsString = "document.getElementById('userId').value = '15656514046';document.getElementById('passwd').value = 'z920618'"
        //本地的类型代码调用js
        webView.stringByEvaluatingJavaScriptFromString(jsString)
    }
    
   //webview   代理方法
    func webViewDidStartLoad(webView: UIWebView) {
        SVProgressHUD.show()
    }
    func webViewDidFinishLoad(webView: UIWebView) {
        let jsString = "document.title"
        let webTitle = webView.stringByEvaluatingJavaScriptFromString(jsString)
        title = webTitle
        SVProgressHUD.dismiss()
    }
    
}
